<template>
  <div class="_wz_hiddenPerilsAdd">
    <div style="padding: 10px 0 0 0;">
      <Form ref="formAdd" :model="formData" label-position="right" :label-width="100" :rules="ruleValidate">

        <Card dis-hover :bordered="true" style="margin:0 0 10px;">
          <p slot="title">隐患部位</p>
          <Row>
            <i-col span="8">
              <FormItem label="运输企业" prop="deptUuid">
                <i-select v-model="formData.deptUuid" filterable placeholder="请选择运输企业" clearable @on-change="deptFun">
                  <Option v-for="item in subDeptList" :value="item.uuid" :key="item.uuid">{{ item.deptName }}</Option>
                </i-select>
              </FormItem>
            </i-col>
            <i-col span="8">
              <FormItem label="隐患类型" prop="riskPositionType">
                <i-select v-model.number="formData.riskPositionType" filterable placeholder="请选择隐患类型" clearable @on-change="screeningCascade">
                  <Option :value="1">基础管理</Option>
                  <Option :value="2">设备设施</Option>
                  <Option :value="3">作业活动</Option>
                </i-select>
              </FormItem>
            </i-col>
            <i-col span="8">
              <FormItem label="隐患部位" prop="riskPositionUuid">
                <i-select v-model="formData.riskPositionUuid" filterable placeholder="请选择隐患部位" clearable :label-in-value="true" @on-change="public.selectName($event,formData,'riskPositionName')">
                  <Option v-for="item in riskPositionList" :value="item.uuid" :key="item.uuid">{{ item.riskPositionName }}</Option>
                </i-select>
              </FormItem>
            </i-col>
          </Row>
        </Card>
        <detailsHiddenParts :selectdata="formData.riskPositionUuid"></detailsHiddenParts>
        <Card dis-hover :bordered="true" style="margin:0 0 10px;">
          <p slot="title">隐患排查</p>
          <Row>
            <i-col span="8">
              <FormItem label="隐患排查" prop="riskInspectName">
                <i-input v-model="formData.riskInspectName" placeholder="请填写隐患排查" clearable />
              </FormItem>
            </i-col>
            <i-col span="8">
              <FormItem label="排查部门" prop="riskInspectInOrgUuid">
                <i-select v-model="formData.riskInspectInOrgUuid" filterable placeholder="请选择排查部门" clearable  @on-change="systemUserListFun">
                  <Option v-for="item in coreInOrgList" :value="item.uuid" :key="item.uuid">{{ item.inOrgName }}</Option>
                </i-select>
              </FormItem>
            </i-col>
            <i-col span="8">
              <FormItem label="排查人员" prop="riskInspectUserUuid">
                <i-select v-model="formData.riskInspectUserUuid" filterable placeholder="请选择排查人" clearable>
                  <Option v-for="item in systemUserList" :value="item.uuid" :key="item.uuid">{{ item.name }}</Option>
                </i-select>
              </FormItem>
            </i-col>
            <i-col span="8">
              <FormItem label="排查时间" prop="dateSearchRange">
                <DatePicker v-model="formData.dateSearchRange" type="daterange" placeholder="请选择起始日期" style="width: 100%;"></DatePicker>
              </FormItem>
            </i-col>
          </Row>
          <FormItem label="排查备注" prop="riskInspectContext">
            <i-input v-model="formData.riskInspectContext" placeholder="请填写排查备注" type="textarea" :autosize="{minRows: 5,maxRows: 5}" />
          </FormItem>
        </Card>
      </Form>
    </div>

    <div class="demo-drawer-footer" style="z-index: 99;">
      <Button style="margin-right: 8px" @click="$emit('closeFun', false)">取消</Button>
      <Button type="primary" @click="addSubmit('formAdd')">提交</Button>
    </div>

    <Spin size="large" fix v-if="spinShow"></Spin>

  </div>
</template>
<script>
import detailsHiddenParts from "./detailsHiddenParts";
export default {
  props: ['selectData'],//接收来自父组件的数据
  components: {
    detailsHiddenParts,//隐患部位详情
  },
  data() {
    return {
      spinShow: false,//本页加载
      formData: {},
      ruleValidate: {//正则
        riskInspectInOrgUuid: [
          {required: true, message: '请选择排查部门', trigger: 'blur'}
        ],
        riskPositionType: [
          {type:'number',required: true, message: '请选择隐患类型', trigger: 'blur'}
        ],
        riskInspectName: [
          {required: true, message: '请填写隐患排查', trigger: 'blur'}
        ],
        deptUuid: [
          {required: true, message: '请选择运输企业', trigger: 'blur'}
        ],
        riskPositionUuid: [
          {required: true, message: '请选择隐患部位', trigger: 'blur'}
        ],
        riskInspectUserUuid: [
          {required: true, message: '请选择排查人员', trigger: 'blur'}
        ],
        riskInspectVehicleUuid: [
          {required: true, message: '请选择排查车辆', trigger: 'blur'}
        ],
        dateSearchRange: [{
          type: 'array',
          required: true,
          message: '请输入起止日期',
          fields: {
            0: {type: 'date', required: true, message: '请输入起止日期'},
            1: {type: 'date', required: true, message: '请输入起止日期'}
          }
        }],
      },
      riskPositionList:[],//隐患清单
      subDeptList:[],//企业
      systemUserList:[],//人员
      coreInOrgList:[],//部门

    }
  },
  beforeCreate() {//beforeCreate创建前状态

  },
  created() {//created创建完毕状态

  },
  beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

  },
  mounted() {//mounted 挂载结束状态
    var that = this;
    that.formData = that.selectData;
    that.formData.dateSearchRange = [new Date(that.formData.riskInspectStartDate),new Date(that.formData.riskInspectEndDate)]

    //运输企业
    that.axios.post(that.apiUrl.coreDeptCoreDeptList, {}).then(res => {
      if (!!res) {
        that.subDeptList = res.data.data;
        if (!that.selectData.id) {
          that.$set(that.formData, 'deptUuid', that.subDeptList[0].uuid)//赋值
          that.deptFun(that.formData.deptUuid)
          // that.screeningCascade(that.formData.deptUuid)
        }
      }
    }).catch(err => {
      console.log("失败", err)
    })


  },
  methods: {//执行的方法
    /*
    * 1.企业 类型 -->部位
    * 2.企业 --> 部门
    * 3.部门 --> 人员
    * */
    //企业级联 部门
    deptFun(data){
      let that = this;
      that.screeningCascade();//级联 隐患部位
      if(!!data){
        //级联部门
        that.axios.post(that.apiUrl.coreInOrgCoreInOrgList, {deptUuid:that.formData.deptUuid,authDataState:true}).then(res => {
          if (!!res) {
            that.coreInOrgList = res.data.data;
            if(that.coreInOrgList.length > 0){
              if (!that.selectData.id) {

                that.$set(that.formData, 'riskInspectInOrgUuid', that.coreInOrgList[0].uuid)//赋值
                that.systemUserListFun(that.formData.riskInspectInOrgUuid);
              }
            }else{
              that.coreInOrgList = [];//部门
              that.$set(that.formData, 'riskInspectInOrgUuid', '')//赋值  //所选部门
              that.systemUserList = [];//人员
              that.$set(that.formData, 'riskInspectUserUuid', '')//赋值  //所选人员
            }
          }
        }).catch(err => {
          console.log("失败", err)
        })


      }else{
        that.coreInOrgList = [];//部门
        that.$set(that.formData, 'riskInspectInOrgUuid', '')//赋值  //所选部门
        that.systemUserList = [];//人员
        that.$set(that.formData, 'riskInspectUserUuid', '')//赋值  //所选人员
      }
    },
    //部门级联人
    systemUserListFun(data){
      let that = this;
      if(!!data){
        let inOrgType = '';//获取部门类型
        that.coreInOrgList.forEach(function(k, i) {
          if(data == k.uuid){
            inOrgType = k.inOrgType;
            return false;
          }
        })
        if(inOrgType == 1){//管理型单位  //人员列表
          that.axios.post(that.apiUrl.systemUserSystemUserList, {inOrgUuid:that.formData.riskInspectInOrgUuid,authDataState:true}).then(res => {
            if (!!res) {
              that.systemUserList = res.data.data;
              if(that.systemUserList.length > 0){
                if (!that.selectData.id) {
                  that.$set(that.formData, 'riskInspectUserUuid', that.systemUserList[0].uuid)//赋值
                }
              }else{
                that.$set(that.formData, 'riskInspectUserUuid', '')//赋值
              }
            }
          }).catch(err => {
            console.log("失败", err)
          })
        }else if(inOrgType == 2){//生产型单位  //驾培人员
          that.axios.post(that.apiUrl.coreDriverCoreDriverList, {inOrgUuid:that.formData.riskInspectInOrgUuid,authDataState:true}).then(res => {
            if (!!res) {
              that.systemUserList = res.data.data;
              if(that.systemUserList.length > 0){
                if (!that.selectData.id) {
                  that.$set(that.formData, 'riskInspectUserUuid', that.systemUserList[0].uuid)//赋值
                }
              }else{
                that.$set(that.formData, 'riskInspectUserUuid', '')//赋值
              }
            }
          }).catch(err => {
            console.log("失败", err)
          })
        }
      }else{
        that.systemUserList = [];//人员
        that.$set(that.formData, 'riskInspectUserUuid', '')//赋值
      }
    },
    //排查周期 级联
    screeningCascade(){
      let that = this;
      if(!!that.formData.riskPositionType && !!that.formData.deptUuid){
        //隐患清单
        that.axios.post(that.apiUrl.riskPositionRiskPositionList, {deptUuid:that.formData.deptUuid,riskPositionType:that.formData.riskPositionType}).then(res => {
          if (!!res) {
            that.riskPositionList = res.data.data;
          }
        }).catch(err => {
          console.log("失败", err)
        })
      }else{
        that.riskPositionList = [];
        that.$set(that.formData, 'riskPositionUuid', '')//赋值
      }
    },
    //提交表单
    addSubmit(data) {
      let that = this;
      that.$refs[data].validate((valid) => {
        if (valid) {//正则判断使用
          that.formData.riskInspectStartDate = that.public.timeFormatting(that.formData.dateSearchRange[0], "yyyy-MM-dd");
          that.formData.riskInspectEndDate = that.public.timeFormatting(that.formData.dateSearchRange[1], "yyyy-MM-dd");
          that.spinShow = true;//显示加载
          that.axios.post(that.apiUrl.riskInspectRiskInspectSaveOrUpdate, that.formData).then(res => {
            that.spinShow = false;
            if (!!res) {
              that.$Message.success("操作成功");
              that.$emit("closeFun", true)
            }
          }).catch(err => {
            console.log('失败', err);
          })
        } else {
          that.$Message.error('请正确填写表单');
        }
      })
    },

  },
  watch: {//监听

  },
  beforeUpdate() {//beforeUpdate 更新前状态

  },
  updated() {//updated 更新完成状态

  },
  beforeDestroy() {//beforeDestroy 销毁前状态

  },
  destroyed() {//destroyed 销毁完成状态

  }
}
</script>
<style lang="less">
._wz_hiddenPerilsAdd {
  //height: 100%;
  //padding-bottom: 53px;
}
</style>
